const express = require('express');
const router = express.Router();
const multer = require('multer'); // 导入Multer中间件库
const fs = require('fs');

router.post('/upload', multer({ dest: 'uploadImg' }).single('img'), (req, res) => {
    newOriginalname = Buffer.from(req.file.originalname, "latin1").toString(
        "utf8"
    );
    //    判断是否有数据
    if (req.file) {
        // console.log(req.file)
        let oldPath = req.file.path;
        let newPath = "public/images/" + `${newOriginalname}`
        //    重命名
        fs.renameSync(oldPath, newPath);
        res.send({
            err: null,
            code: 200,
            //保存到本地
            url: "http://localhost:3005/images/" + `${newOriginalname}` // 复制URL链接直接浏览器可以访问
        });

    }
});
module.exports = router